package com.sta.oldman.controller;

import com.sta.oldman.common.ResponseVO;
import com.sta.oldman.model.req.*;
import com.sta.oldman.service.UserService;
import com.sta.oldman.utils.JWT;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: LunBoWang
 * @description: 用户controller
 * @date: 2025/7/14 上午10:06
 * @version: 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping("/register")
    public ResponseVO register(@RequestBody @Validated RegisterReq req) {
        return userService.register(req);
    }

    @RequestMapping("/login/phone")
    public ResponseVO loginByPhone(@RequestBody @Validated LoginByPhoneReq req) {
        return userService.loginByPhone(req);
    }

    @RequestMapping("/login/email")
    public ResponseVO loginByEmail(@RequestBody @Validated LoginByEmailReq req) {
        return userService.loginByEmail(req);
    }

    @RequestMapping("/login/username")
    public ResponseVO loginByUsername(@RequestBody @Validated LoginByUsernameReq req) {
        return userService.loginByUsername(req);
    }

    @RequestMapping("/binding/phone")
    public ResponseVO bindingPhone(@RequestBody @Validated BindingPhoneReq req,
                                   HttpServletRequest request) {
        req.setUserId(getUserId(request));
        return userService.bindingPhone(req);
    }

    @RequestMapping("/binding/email")
    public ResponseVO bindingEmail(@RequestBody @Validated BindingEmailReq req,
                                   HttpServletRequest request) {
        req.setUserId(getUserId(request));
        return userService.bindingEmail(req);
    }

    @RequestMapping("/binding/address")
    public ResponseVO bindingAddress(@RequestBody @Validated BindingAddressReq req,
                                   HttpServletRequest request) {
        req.setUserId(getUserId(request));
        return userService.bindingAddress(req);
    }

    @RequestMapping("/binding/imei")
    public ResponseVO bindingAddress(@RequestBody @Validated BindingImeiReq req,
                                     HttpServletRequest request) {
        req.setUserId(getUserId(request));
        return userService.bindingImei(req);
    }

    @RequestMapping("/getUserInfo")
    public ResponseVO getUserInfo(HttpServletRequest request) {
        Long userId = getUserId(request);
        return userService.getUserInfo(userId);
    }

    @RequestMapping("/getCode")
    public ResponseVO getCode(@RequestBody @Validated GetCodeReq req) {
        return userService.getCode(req);
    }

    private Long getUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJWT(token);
        return (Long) claims.get("userId");
    }
}
